package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.core.MachineDao;
import com.tbit.uqbike.client.dao.core.MachineFeeDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.service.AdAccountFeeService;
import com.tbit.uqbike.client.service.MachineFeeService;
import com.tbit.uqbike.client.service.RideCardUserService;
import com.tbit.uqbike.client.service.VipCardLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("machineFeeService")
public class MachineFeeServiceImpl implements MachineFeeService {
    @Autowired
    private MachineFeeDao machineFeeDao;
    @Autowired
    private VipCardLogService vipCardLogService;
    @Autowired
    private RideCardUserService rideCardUserService;
    @Autowired
    private MachineDao machineDao;
    @Autowired
    private AdAccountFeeService adAccountFeeService;

    @Override
    public AdAccountFee getByAccountId(Integer accountId, Integer machineId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("machineId", machineId);
        return machineFeeDao.getByAccountId(map);
    }

    @Override
    public void getDiscountByMachineNO(User user, String userCode, Map<String, Object> map) {
        Machine machine = machineDao.getByUserCode(userCode);
        if (machine == null) {
            throw new BaseException("-30005", "设备不存在");
        }
        AdAccountFee adAccountFee = adAccountFeeService.getByAccountId(machine.getAccountId(), machine.getMachineId());
        Integer feeId = 0;
        if (adAccountFee != null && adAccountFee.getName() != null) {
            feeId = adAccountFee.getFeeId();
        }

        List<RideCardUser> rideCardUsers = rideCardUserService.getByUserId(user.getUserId(), machine.getAccountId());
        RideCardUser rideCardUserDB = null;
        if (rideCardUsers.size() > 0) {
            for (RideCardUser r : rideCardUsers) {
                if (r.getFeeName() == null) {
                    r.setFeeName("全车型");
                    rideCardUserDB = r;
                }

                if (r.getFeeId().equals(feeId)) {
                    rideCardUserDB = r;
                    break;
                }
            }
            map.put("rideCard", rideCardUserDB);
        }
        List<VipCardLog> vipCardLogs = vipCardLogService.getByTime(machine.getAccountId(), user.getUserId());

        if (vipCardLogs.size() > 0) {
            VipCardLog vipCardLog = null;
            for (VipCardLog vip : vipCardLogs) {
                if (vip.getFeeName() == null) {
                    vip.setFeeName("全车型");
                    vipCardLog = vip;
                }
                if (vip.getFeeId().equals(feeId)) {
                    vipCardLog = vip;
                    break;
                }
            }
            map.put("vipCard", vipCardLog);
        }


    }

}  